package org.dawn.deveco.plugin.resourceeditor.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.dawn.deveco.plugin.resourceeditor.common.Constants;
import org.dawn.deveco.plugin.resourceeditor.model.item.BaseItem;
import org.dawn.deveco.plugin.resourceeditor.model.item.SimpleItem;
import org.dawn.deveco.plugin.resourceeditor.utils.Logger;

import javax.swing.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Vector;

public class SimpleResourceContent extends BaseResourceContent {
    private static final Logger LOG = Logger.getInstance(SimpleResourceContent.class);

    public SimpleResourceContent() {
        this("");
    }

    public SimpleResourceContent(String category) {
        setCategory(category);
        setColumns(new ColumnConfig("Name", String.class, true, true),
                new ColumnConfig("Value", String.class, true, true),
                new ColumnConfig("", Icon.class, true, false, false, "删除按钮"));
    }

    public Vector<Vector> getJTableContent(String group) {
        List<? extends BaseItem> itemList = this.getGroupData(group);
        Vector<Vector> list = new Vector<>();
        for (final BaseItem e : itemList) {
            SimpleItem item = (SimpleItem) e;
            Vector<Object> vec = new Vector<>(3);
            vec.add(item.name);
            vec.add(item.value);
            vec.add(null);   // Action 列
            list.add(vec);
        }
        return list;
    }

    public boolean save() {
        boolean ret = false;
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode root = mapper.createObjectNode();

        for (String group : this.tableDataByGroup.keySet()) {
            Vector<Vector> data = this.tableDataByGroup.get(group);
            toJson(root, group, data);
        }

        String filename = Constants.RESOURCE_PATH_MAP.get(this.getCategory());
        Path filePath = Paths.get(Constants.RESOURCE_ROOT_PATH, filename);
        try {
//            mapper.writeValue(filePath.toFile(), root);
            String jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
//            LOG.infoF("save", "category: %s, data:\n%s", this.getCategory(), jsonStr);
            Files.writeString(filePath, jsonStr, StandardCharsets.UTF_8);
            ret = true;
        } catch (Exception e) {
            LOG.error("failed to save resource file: " + filePath, e);
        }
        return ret;
    }

    private ObjectNode toJson(ObjectNode root, String group, Vector<Vector> data) {
        ArrayNode arrayNode = root.putArray(group);
        for (Vector row : data) {
            ObjectNode itemNode = arrayNode.addObject();
            itemNode.put("name", (String) row.get(0));
            itemNode.put("value", (String) row.get(1));
        }
        return root;
    }
}
